//
//  TwoViewController.m
//  练习
//
//  Created by 王成龙 on 16/5/28.
//  Copyright © 2016年 wcl. All rights reserved.
//

#import "TwoViewController.h"
@interface PhoneData :NSObject
@property (strong,nonatomic) NSString *name;
@property (strong,nonatomic) NSString *tel ;

@end
@implementation PhoneData
@end
@interface TwoViewController ()<UITableViewDataSource>
{
    NSArray *data;
    
    
}
@end

@implementation TwoViewController
- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.title=@"联系人";
    self.view.backgroundColor=[UIColor blueColor];//背景颜色
    
    NSBundle*bundle=[NSBundle mainBundle];
    NSURL*url=[bundle URLForResource:@"PIM00001" withExtension:@"vcf"];
    NSString *text = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    // NSLog(@"读取数据：%@", text);
    
    NSArray *arr = [text componentsSeparatedByString:@"\n"];
    PhoneData*item;
    NSMutableArray *tmp = [NSMutableArray new];
    for(NSString*p in arr){
        if([p hasPrefix:@"BEGIN"])
        {
            item=[PhoneData new];
        }else if([p hasPrefix:@"END"])
        {
            [tmp addObject:item];
        }else if([p hasPrefix:@"TEL"])
        {
            
            NSArray *components=[p componentsSeparatedByString:@":"];
            NSString*app=components[1];
            item.tel=app;
        }
        else if ([p hasPrefix:@"FN;"]){
            NSArray *upperComponents = [p componentsSeparatedByString:@":"];
            NSString *name = upperComponents[1];
            name = [name stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
            name = [name stringByRemovingPercentEncoding];
            
            // NSlog(@"FN:%@",name);
            item.name = name;
        }
        
        
        data=tmp;
    }
   }


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return data.count;
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *call =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    PhoneData *d =data[indexPath.row];
    call.textLabel.text = d.tel;
    call.detailTextLabel.text =d.name;
    
    return call;
    
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}



@end
